package com.itheima.d3_buffer;

import java.io.*;

//需求：分别使用低级字节流和高级字节缓冲流拷贝大视频，记录耗时
public class Demo06 {
    public static void main(String[] args) throws Exception {
        //baseStream(); //34551毫秒

        //bufferedStream(); //9437毫秒
    }

    public static void bufferedStream() throws IOException {
        //使用缓冲字节流进行拷贝
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\姜志成\\Desktop\\test\\1.zip"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\姜志成\\Desktop\\test\\2.zip"));

        //一次读取一个字节数组
        byte[] bys = new byte[1024];
        int len;

        long start = System.currentTimeMillis();
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }
        bos.flush();

        long end = System.currentTimeMillis();
        System.out.println("基础字节流拷贝消耗时间：" + (end - start) + "毫秒");
        bos.close();
        bis.close();
    }

    public static void baseStream() throws IOException {
        //1.用基础的文件字节流拷贝，统计时间
        FileInputStream fis = new FileInputStream("C:\\Users\\姜志成\\Desktop\\test\\1.zip");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\姜志成\\Desktop\\test\\2.zip");
        byte[] bys = new byte[1024];
        int len;

        long start = System.currentTimeMillis();

        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }

        //刷新和释放资源
        fos.flush();

        long end = System.currentTimeMillis();
        System.out.println("基础字节流拷贝消耗时间：" + (end - start) + "毫秒");
        fos.close();
        fis.close();
    }
}
